/* 头文件声明区 */
#include <STC15F2K60S2.H>
#include <intrins.h>
#include <Init.h>
#include <Key.h>
#include <Seg.h>
#include <Led.h>
#include <ds1302.h>
#include <onewire.h>

#define uchar unsigned char
#define uint unsigned int

/* 变量声明区 */
uchar Key_Val,Key_Down,Key_Up,Key_Old;
uchar Key_Slow_Down;
uchar Seg_Buf[8] = {10,10,10,10,10,10,10,10};
uchar Seg_Point[8] = {0,0,0,0,0,0,0,0};
uchar Seg_Pos;
uint Seg_Slow_Down;
uchar ucLed[8] = {0,0,0,0,0,0,0,0};
uchar Temperature_Time_Set[4] = {1,5,30,60};//温度控制时间
uchar Temperature_Time_Set_Control;//温度时间控制值
uchar Temperature_Time_Set_Index;//温度设置数组索引值
uchar Seg_Disp_Mode;//0-参数设置界面 1-时钟显示界面 2-温度采集界面
uchar Set_RTC[3] = {23,59,50};//上电初始化时间
uint Timer1000Ms;//1000ms标志位
uint Timer500Ms;//500ms标志位
bit Seg_Star;//闪烁标志位
uchar Temperature[10] = {0,0,0,0,0,0,0,0,0,0};//采集十个温度
uchar Temperature_Index;//由0~9；
uint Tick;//判断是否到采集时间
bit Led_Star_Flag;//Led闪烁启动标志位
bit Led_Star;//Led闪烁标志位
bit Seg_2_Flag;//温度模式启动标志位

/* 键盘处理函数 */
void Key_Proc(){
	uchar i;
    if(Key_Slow_Down)return;
    Key_Slow_Down = 1;

    Key_Val = Key_Read();
    Key_Down = Key_Val & (Key_Val ^ Key_Old);
    Key_Up = ~Key_Val & (Key_Val ^ Key_Old);
    Key_Old = Key_Val;

	switch(Key_Down){
		case 4:
			if(Seg_Disp_Mode == 0){
				if(++Temperature_Time_Set_Index == 4)Temperature_Time_Set_Index = 0;		
			}
			break;
		case 5:
			if(Seg_Disp_Mode == 0){
				Temperature_Time_Set_Control = Temperature_Time_Set[Temperature_Time_Set_Index];
				Temperature_Time_Set_Index = 0;
				Seg_Disp_Mode = 1;
			}
			break;
		case 6:
			if(Seg_Disp_Mode == 2){
				Led_Star_Flag = Led_Star = 0;			
				Seg_2_Flag ^= 1;
			}
			break;
		case 7:
			if(Seg_Disp_Mode == 2){
				for(i=0;i<10;i++)
					Temperature[i] = 0;
				Temperature_Index = 0;
				Seg_2_Flag = 0;
				Seg_Disp_Mode = 0;
			}			
			break;
	}
}

/* 数码管处理函数 */
void Seg_Proc(){
	uchar i;
    if(Seg_Slow_Down)return;
    Seg_Slow_Down = 1;
	
	Read_Rtc(Set_RTC);

	switch(Seg_Disp_Mode){
		case 0:
			for(i=0;i<5;i++)
				Seg_Buf[i] = 10;
			Seg_Buf[5] = 11;//11-
			Seg_Buf[6] = Temperature_Time_Set[Temperature_Time_Set_Index] / 10 % 10;
			Seg_Buf[7] = Temperature_Time_Set[Temperature_Time_Set_Index] % 10;
			break;
		case 1:
			for(i=0;i<3;i++){
				Seg_Buf[3*i] = Set_RTC[i] / 10 % 10;
				Seg_Buf[3*i+1] = Set_RTC[i] % 10;
			}
			Seg_Buf[2] = Seg_Buf[5] = 11;	
			Seg_Buf[2] = Seg_Buf[5] = Seg_Star ? 10 : 11;
			break;
		case 2:
			Seg_Buf[0] = 11;
			Seg_Buf[1] = 0;
			Seg_Buf[2] = Temperature_Index;
			Seg_Buf[3] = Seg_Buf[4] = 10;
			Seg_Buf[5] = 11;
			Seg_Buf[6] = Temperature[Temperature_Index] / 10 % 10;
			Seg_Buf[7] = Temperature[Temperature_Index] % 10;
			if(Seg_2_Flag){
				if(++Temperature_Index == 10)Temperature_Index = 9;
			}
			break;
	}
}

/* Led处理函数 */
void Led_Proc(){
	ucLed[0] = Led_Star;
}

/* 定时器0初始化函数 */
void Timer0Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x18;		//设置定时初始值
	TH0 = 0xFC;		//设置定时初始值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
    ET0 = 1;
    EA = 1;
}

/* 定时器0服务函数 */
void Timer0Server() interrupt 1{
    if(++Key_Slow_Down == 10)Key_Slow_Down = 0;
    if(++Seg_Slow_Down == 500)Seg_Slow_Down = 0;
    if(++Seg_Pos == 8)Seg_Pos = 0;
    Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
    Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
	
	if(Seg_Disp_Mode == 1){
		if(++Timer1000Ms == 1000){
			Timer1000Ms = 0;
			Seg_Star ^= 1;
		}
		
		if(++Tick == (Temperature_Time_Set_Control*1000)){
			Tick = 0;
			Temperature[Temperature_Index++] = Rd_Temperature();
			if(Temperature_Index == 10){
				Temperature_Index = 0;
				Led_Star_Flag ^= 1;
				Seg_Disp_Mode = 2;
			}
		}
	}
	
	if(Led_Star_Flag){
		if(++Timer500Ms == 500){
			Timer500Ms = 0;
			Led_Star ^= 1;
		}
	
	}
	
	
	
}

void Delay750ms()		//@12.000MHz
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 35;
	j = 51;
	k = 182;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}


/* 主函数 */
void main(){
		Rd_Temperature();
		Delay750ms();
    System_Init();
    Timer0Init();
		Set_Rtc(Set_RTC);
    while(1){
        Key_Proc();
        Seg_Proc();
        Led_Proc();
    }
}
